package com.jee.ssm.modules.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.model.Banner;
import com.jee.ssm.model.Banner_Class;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.modules.ssm.services.BannerService;
import com.jee.ssm.modules.ssm.services.Banner_ClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/banner_class")
public class Banner_ClassController extends AdminBaseController<Banner_Class>{
    /**
     * 进入用户添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("banner_class:add")
    public ModelAndView add(ModelMap model){
        model.put("longId", UUIDFactory.getStringId());
        return new ModelAndView("manager/banner_class/add",model);
    }
    /**
     * 进入用户编辑页面
     * @param model 返回用户的容器
     * @param id 用户id
     * @return 编辑页面
     * @throws Exception 进入编辑页面失败
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("banner_class:edit")
    public ModelAndView edit(ModelMap model, @RequestParam String id) {
        model.put("data",banner_classService.selectById(id));
        return new ModelAndView("manager/banner_class/edit",model);
    }
    /**
     * 角色添加
     * @param banner_class 带id的用户对象
     * @return 成功状态
     * @throws Exception 数据添加异常
     */
    @ResponseBody
    @RequestMapping(value="/save")
    @AdminControllerLog(description = "用户添加" )
    @RequiresPermissions("banner_class:add")
    public Tip save(Banner_Class banner_class) throws Exception{
        banner_classService.insert(banner_class);
        return new Tip();
    }

    /**
     * 根据 id 修改用户
     * @param banner_class 带id的用户对象
     * @return 成功状态
     * @throws Exception 数据修改异常
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @AdminControllerLog(description = "用户编辑" )
    @RequiresPermissions("banner_class:edit")
    public Tip update(Banner_Class banner_class) throws Exception {
        banner_classService.updateById(banner_class);
        return new Tip();
    }
    /**
     * 根据 id 删除用户
     * @param id 用户id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @AdminControllerLog(description = "删除用户" )
    @RequiresPermissions("banner_class:delete")
    public Tip delete(@RequestParam String id) {

        try {
            banner_classService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1);
        }
    }
    /**
     * 根据 id 列表批量删除
     * @param ids 用户id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @AdminControllerLog(description = "批量删除用户" )
    @RequiresPermissions("banner_class:delete")
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            banner_classService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1);
        }
    }
    /**
     * 根据 id 查找用户
     * @param id 用户id
     * @return 用户对象 jsonj
     * @throws Exception 查找用户失败
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    public Banner_Class find(@RequestParam String id) {

        return banner_classService.selectById(id);
    }

    /**
     * 获取用户列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 用户列表页面
     * @throws Exception 获取用户列表失败
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("banner_class:list")
    public ModelAndView list(HttpServletRequest request,@RequestParam Integer page, @RequestParam Integer size) throws Exception {

        ParamMap paramMap = new ParamMap(request);
        PageInfo<Banner_Class> pageInfo =  banner_classService.list(paramMap,page,size);

        paramMap.put("pageInfo",pageInfo);
        return new ModelAndView("manager/banner_class/list",paramMap);
    }





    //----------------------------- property ----------------------------------
    @Resource
    private Banner_ClassService banner_classService;
}
